﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Lion.WeiXin.Core.Data;
using Lion.WeiXin.Core.Models;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Lion.WeiXin.Tools.Log;
namespace Lion.WeiXin.Controllers
{
      [PermissionFilter]
    public class UserInfoController : Controller
    {
        IUserInfo iuser = new UserInfoDAO();
        Lion.WeiXin.Core.Data.IDictionary idic = new DictionaryDAO();


        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        ///  获取分页列表数据
        /// </summary>
        /// <param name="keyword"></param>
        /// <param name="msgType"></param>
        /// <param name="currentPage"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public string GetPageList(string userName, int currentPage, int pageSize)
        {
            //数据量少的，直接查全部数据分页展示           
            List<UserInfoModel> list = iuser.GetList(string.Empty);
            int totalCount = list.Count;
            userName = HttpUtility.UrlDecode(userName);
            //如果传入条件参数
            if (!string.IsNullOrEmpty(userName))
            {
               
                 list = list.Where(n => n.UserName.Contains(userName)).OrderByDescending(n => n.AID).Skip((currentPage - 1) * pageSize).Take(pageSize).ToList();
               
            }
            else
            {
                list = list.OrderByDescending(n => n.AID).Skip((currentPage - 1) * pageSize).Take(pageSize).ToList();
            }

            string jsonResult = JsonConvert.SerializeObject(list, new IsoDateTimeConverter());
            jsonResult = "{\"total\":" + totalCount + "," + "\"data\":" + jsonResult + "}";
            return jsonResult;
        }


        public ActionResult Show()
        {
            string ID = Request.QueryString["ID"];
            if (string.IsNullOrEmpty(ID))
            {
                return Redirect(Request.RawUrl);
            }

            int modelID = int.Parse(ID);
            UserInfoModel model = iuser.GetModel(modelID);
            return View(model);
        }


        public ActionResult Create()
        {
            return View();
        }

        public ActionResult Save(FormCollection form)
        {
            string action = Request.QueryString["Action"];
            string result = SaveStateEnum.OK.ToString();

            UserInfoModel model = new UserInfoModel();
            TryUpdateModel(model);
            model.Creator = CurrentUser.UserName;
            try
            {
                if (action == ActionEnum.Create.ToString())
                {
                    if (!iuser.Exists(model.UserName))
                    {
                        iuser.Add(model);
                    }
                    else
                    {

                        result ="用户名已存在";
                    }
                }
                else if (action == ActionEnum.Update.ToString())
                {
                    iuser.Update(model);
                }
                else
                {
                    result = SaveStateEnum.NO.ToString();
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex, "数据{0}失败", action);
                result = SaveStateEnum.NO.ToString();
            }

            return Content(result);
        }

        public ActionResult Delete(string IDs)
        {
            string result = SaveStateEnum.NO.ToString();
            try
            {
                if (!string.IsNullOrEmpty(IDs))
                {
                    iuser.DeleteList(IDs);
                    result = SaveStateEnum.OK.ToString();
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex, "删除数据失败!");
            }
            return Content(result);
        }


    }
}
